package com.comdosoft.comdoandroidbase.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.comdosoft.comdoandroidbase.R;
import com.comdosoft.comdoandroidbase.adapter.SwipeMenuListViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yewei on 2015/9/18.
 */
public class SwipeMenuListViewFragment extends Fragment {


    private SwipeMenuListView smlv;
    private SwipeMenuListViewAdapter mAdapter;
    private List<String> mList = new ArrayList<String>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_swipemenulistview, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {

        smlv = (SwipeMenuListView) view.findViewById(R.id.swpie);

        getData(10);
        mAdapter = new SwipeMenuListViewAdapter(getActivity(), mList);
        smlv.setAdapter(mAdapter);
        // step 1. create a MenuCreator
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {

                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getActivity());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(150);
                // set a icon
                deleteItem.setIcon(R.drawable.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);

            }
        };
        // set creator
        smlv.setMenuCreator(creator);

        // step 2. listener item click event
        smlv.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(final int position, SwipeMenu menu,
                                        int index) {
                mList.remove(position);
                mAdapter.notifyDataSetChanged();
                Toast.makeText(getActivity(),"删除第"+position+"个",Toast.LENGTH_SHORT);
            }
        });

    }

    private List<String> getData(int page) {
        mList.clear();
        for (int i = 0; i < page * 10; i++) {
            mList.add(i + "");
        }
        return mList;
    }
}
